#!/usr/bin/python
#import the required libs.
import random;

#set up the main variables that have yet to be implemeted
playerScore = 0
totalProblems = 10
tries = 0
mainLoop = True

#define functions
def bye():
    print("\nThank you for using Math Drill.\n");
    print("goto: http://brainbacon.blogspot.com\n");
    print("(c) Brain Bacon Crew:");
    print("\njohnwedd, PhearTheCeal, and KingCritter");
    exit
    mainLoop = False

def division(): 
    a = random.randrange(20,40)
    b = random.randrange(1,20)
    try:
        answer = float(input(str(a) + " / " + str(b) + " is? "))
    except ValueError:
        answer = 'wrong'
    if answer != (a/b):
        print("\nWrong! Correct answer is " + str(a/b))
        return 0
    else:
        print("\nCorrect!")
        return 1
        
def multiplication(): 
    a = random.randrange(1,12)
    b = random.randrange(1,12)
    try:
        answer = int(input(str(a) + " X " + str(b) + " is? "))
    except ValueError:
        answer = 'wrong'
    if answer != (a*b):
        print("\nWrong! Correct answer is " + str(a*b))
        return 0
    else:
        print("\nCorrect!")
        return 1

def addition(): 
    a = random.randrange(1,100)
    b = random.randrange(1,100)
    try:
        answer = int(input(str(a) + " + " + str(b) + " is? "))
    except ValueError:
        answer = 'wrong'
    if answer != (a+b):
        print("\nWrong! Correct answer is " + str(a+b))
        return 0
    else:
        print("\nCorrect!")
        return 1
        
def subtraction(): 
    a = random.randrange(100,200)
    b = random.randrange(1,100)
    try:
        answer = int(input(str(a) + " - " + str(b) + " is? "))
    except ValueError:
        answer = 'wrong'
    if answer != (a-b):
        print("\nWrong! Correct answer is " + str(a-b))
        return 0
    else:
        print("\nCorrect!")
        return 1

def mix():
    global choices
    r = random.choice(list(choices.keys()))
    return choices[r]()

def mainMenu(tries,totalProblems,choices,playerScore):
    while tries < totalProblems:
        print("\nPick a subject to practice:");
        print("[D]ivison");
        print("[M]ultiplication")
        print("[A]ddition")
        print("[S]ubtraction")
        print("[R]andom")
        print("[E]xit")
        choice = input('Which one?').lower()
        if choice == 'e':
            return 'bye'
        while choices.get(choice) != None and tries < totalProblems:
            playerScore += choices[choice]()
            tries += 1
        else:
            print("\nYeah...what?\n")
        print("\nScoreCard\n" +'='*9, "\n")
        print("Total problems: " + str(totalProblems))
        print("Total correct : " + str(playerScore))
        print(' '*14, "-"*7)
        print("\ndone")

#dictionary setup for easy function retrieval 
choices = {'d':division, 'm':multiplication, 'a':addition, 's':subtraction, 'r':mix}

while mainLoop == True:
    if mainMenu(tries,totalProblems,choices,playerScore) == 'bye':
        mainLoop = False
        bye()

    



